#include "jmessagenotifier.h"
#include <QTECore/jlog.h>
#include <QVariant>

namespace QTE
{

class Q_DECL_HIDDEN JMessageNotifierPrivate
{
public:
};

/*--------------------------------------------------------------------------------------*/

JMessageNotifier::JMessageNotifier(QObject *parent) :
	QObject(parent),
	d_ptr(new JMessageNotifierPrivate())
{

}

JMessageNotifier::~JMessageNotifier()
{
	delete d_ptr;
}

static inline void __warning(const QString &className)
{
	jWarning() << QString("JMessageNotifier::create: The type is not register. (%1)").arg(className);
}

static inline JMessageNotifier *__object(const QString &className, QObject *obj, QObject *parent = nullptr)
{
	auto notifier = J_OCT(JMessageNotifier*, obj);
	if( notifier == nullptr )
		jError() << QString("JMessageNotifier::create: Object create failed. (%1) [type error]:").arg(className) << obj;
	else
		notifier->setParent(parent);
	return notifier;
}

JMessageNotifier *JMessageNotifier::create(const QString &className, QObject *parent)
{
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
	int typeID = QMetaType::type(className.toLocal8Bit());
	if( typeID <= 0 )
	{
		__warning(className);
		return __object(className, nullptr);
	}
	return __object(className, QMetaType::metaObjectForType(typeID)->newInstance(), parent);
#else //qt6
	auto metaType = QMetaType::fromName(className.toLocal8Bit());
	if( not metaType.isValid() )
	{
		__warning(className);
		return __object(className, nullptr);
	}
	return __object(className, metaType.metaObject()->newInstance(), parent);
#endif //qt6
}

QString JMessageNotifier::serviceName() const
{
	return property("serviceName").toString();
}

} //namespace QTE
